﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;

using Prism.Events;

using SensingPlatform.Foundation.Events;

namespace CircumferenceRotatePanelControl.Views
{
    /// <summary>
    /// Interaction logic for ViewA.xaml
    /// </summary>
    public partial class ViewA : UserControl
    {
        public ViewA()
        {
            InitializeComponent();

            try
            {
                //InitChildren();
            }
            catch (Exception ex)
            {

                //throw;
            }
        }
        List<CircumferenceItem> circumferenceItems = [];

        private void InitChildren()
        {
            //panel.Children.Clear();

            circumferenceItems.Add(new CircumferenceItem
            {
                Angle = 20,
                Name = "A",
                ImagePath = "icon/1.png"
            });
            circumferenceItems.Add(new CircumferenceItem
            {
                Angle = 60,
                Name = "B",
                ImagePath = "icon/2.png"
            });
            circumferenceItems.Add(new CircumferenceItem
            {
                Angle = 60,
                Name = "C",
                ImagePath = "icon/3.png"
            });

            panel.circumferenceItems = circumferenceItems;

            AssambleItem();

        }

        public void Initialize(XElement config)
        {
            if (config != null)
            {
            }
        }

        public void StartPage(string query)
        {
            if (!string.IsNullOrEmpty(query))
            {
                //Output.Text += query + Environment.NewLine;
                SensingPlatformEventHelper.TriggerEvent("", this);
            }
        }

        public void StopPage(string query)
        {
            if (!string.IsNullOrEmpty(query))
            {
                //Output.Text += query + Environment.NewLine;
            }
        }

        private void AssambleItem()
        {
            for (int i = 0; i < circumferenceItems.Count; i++)
            {
                CircumferenceItem item = circumferenceItems[i];

                // 创建 Image 对象
                // 假设 item.ImagePath 是类似 "icon/3.png" 的相对路径

                Image itemImage = new Image();

                try
                {
                    // 构建 Uri，指定是相对路径，并启用打包 URI 解析
                    Uri imageUri = new Uri(item.ImagePath, UriKind.Relative);

                    // 创建 BitmapImage 并设置到 Image.Source
                    BitmapImage bitmap = new BitmapImage();
                    bitmap.BeginInit();
                    bitmap.UriSource = imageUri;
                    bitmap.CacheOption = BitmapCacheOption.OnLoad; // 提高加载稳定性
                    bitmap.EndInit();

                    itemImage.Source = bitmap;
                    itemImage.Width = 80;
                    itemImage.Height = 80;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"Failed to load image from path: {item.ImagePath}. Error: {ex.Message}");
                    // 可以选择继续或设置一个默认图片
                }

                // 添加到 Panel 中
                panel.Children.Add(itemImage);
            }
        }


    }
}
